package org.vsg.stock.basic.service.volfigure;

import java.text.DecimalFormat;
import java.util.List;

import org.vsg.stock.core.domain.StockDailyKLine;

/**
 * 
 * @author Bill Vison
 *
 */
public class FigureCounter {
	
	/**
	 * upper close price for recent
	 */
	static public int level1Counter = 0;

	/**
	 * upper close price for short
	 */
	static public int level11Counter = 0;

	
	/**
	 * between open & close for recent
	 */
	static public int level2Counter = 0;

	/**
	 * between open & close for short
	 */
	static public int level21Counter = 0;
	
	/**
	 * below open price for recent
	 */
	static public int level3Counter = 0;

	/**
	 * below open price for short
	 */	
	static public int level31Counter = 0;
	

	static private DecimalFormat nf = new DecimalFormat("##0.000");		
	
	public static void countState(List<StockDailyKLine> stkDailyKLineList , int index) {
		// -- level ---
		StockDailyKLine levelKline = stkDailyKLineList.get(index);
		double closePrice = levelKline.getClose();
		double openPrice = levelKline.getOpen();

		
		
		int offset = 4;
		int offsetStart = index+4;
		
		double maxPrice = openPrice > closePrice ? openPrice : closePrice;
		double minPrice = openPrice > closePrice ? closePrice : openPrice ;
		
		
		// --- find the rece
		double recentPrice = calculateRecent(stkDailyKLineList , offsetStart , 2);
		
		double shortPrice = calculateRecent(stkDailyKLineList , offsetStart + 2 , 7);
		
		
		// --- check recent --
		if (recentPrice >= maxPrice) {
			level1Counter++;
		} else if ( recentPrice < maxPrice && recentPrice >= minPrice  ) {
			level2Counter++;
		} else if (recentPrice > 0){
			level3Counter++;
		}

		if (shortPrice >= maxPrice) {
			level11Counter++;
		} else if ( shortPrice < maxPrice && shortPrice >= minPrice  ) {
			level21Counter++;
		} else if (shortPrice > 0 ){
			level31Counter++;
		}
	}
	
	public static void countState(List<StockDailyKLine> stkDailyKLineList , int standarLevelIndex , int offset) {
		// -- level ---
		StockDailyKLine levelKline = stkDailyKLineList.get(standarLevelIndex);
		double closePrice = levelKline.getClose();
		double openPrice = levelKline.getOpen();
		
		int offsetStart = standarLevelIndex+offset;
		
		
		// --- find the rece
		double recentPrice = calculateRecent(stkDailyKLineList , offsetStart , 2);
		double shortPrice = calculateRecent(stkDailyKLineList , offsetStart + 2 , 7);
		
		double maxPrice = openPrice > closePrice ? openPrice : closePrice;
		double minPrice = openPrice > closePrice ? closePrice : openPrice ;
		//System.out.println( "recentPrice="+recentPrice+" , "+maxPrice+" , "+minPrice );
		
		
		// --- check recent --
		if (recentPrice >= maxPrice) {
			level1Counter++;
		} else if ( recentPrice < maxPrice && recentPrice >= minPrice  ) {
			level2Counter++;
		} else if (recentPrice > 0){
			level3Counter++;
		}

		if (shortPrice >= maxPrice) {
			level11Counter++;
		} else if ( shortPrice < maxPrice && shortPrice >= minPrice  ) {
			level21Counter++;
		} else if (shortPrice > 0 ){
			level31Counter++;
		}
	}
	
	
	private static double calculateRecent( List<StockDailyKLine> stkDailyKLineList , int index , int count) {
		
		double sumprice = 0;
		int total = index+count > stkDailyKLineList.size() ? stkDailyKLineList.size() : index+count;
		int tmpSize = stkDailyKLineList.size() - index;
		count = tmpSize< count ? tmpSize : count;
		for (int i = index ; i < total ;i++) {
			sumprice+= stkDailyKLineList.get(i).getClose();
		}
		if ( count == 0) {
			return 0;
		}
		double avd = sumprice/ count;
		return  Double.parseDouble( nf.format( avd ) );
	}
	
	
	public static String report() {
		StringBuffer log = new StringBuffer();
		log.append("1 Urec:"+level1Counter+", Ushort:"+level11Counter).append("\n");
		log.append("2 Urec:"+level2Counter+", Ushort:"+level21Counter).append("\n");
		log.append("3 Urec:"+level3Counter+", Ushort:"+level31Counter);
		return log.toString();
	}
	
	
}
